package likong.bibliothek.pages.autor;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.util.string.StringValueConversionException;
import org.hibernate.Session;

import likong.bibliothek.obj.Autor;

import db.HibernateHelper;

public class AutorEdit extends AutorAbstract {
	public AutorEdit(final PageParameters params) {
			super(params);
			
			// Load Person
			
			int autorID;
			Autor autor;
			try {
				autorID = params.getInt("autorID");
				autor = HibernateHelper.retrieveObject(Autor.class, autorID); //Autor.fromID(autorID);
			} catch (Exception e) {
				e.printStackTrace();
				this.setRedirect(true);
				this.setResponsePage(AutorWeb.class);
				return;
			}
			
			final CompoundPropertyModel<Autor> model = new CompoundPropertyModel<Autor>(autor);

			add(new Label("vorname", model.bind("vorname")));
			add(new Label("name", model.bind("name")));
			
			// Panel einfügen
			add(new AutorFormPanel("editAutorFormular", model, "Autor aktualisieren") {
				private static final long serialVersionUID = 1L;

				@Override
				protected void onSubmitModelHandle(IModel<Autor> model) {
//					Session session = HibernateHelper.getSessionFactory().getCurrentSession();
//					session.beginTransaction();
//					session.saveOrUpdate(model.getObject());
//					session.getTransaction().commit();
					//session.close();
					
					HibernateHelper.saveOrUpdate(model.getObject());
					
					this.setRedirect(true);
					this.setResponsePage(AutorWeb.class, params);
				}
				
				@Override
				protected void onBackButtonClick() {
					this.setRedirect(true);
					this.setResponsePage(AutorWeb.class, params);
				}
			});
			
		}
	}

